package cn.iocoder.yudao.module.system.controller.admin.a_quest;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.a_quest.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptRespVO;
import cn.iocoder.yudao.module.system.convert.a_quest.QuestConvert;
import cn.iocoder.yudao.module.system.convert.dept.DeptConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.a_quest.QuestDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.a_article.ArticleService;
import cn.iocoder.yudao.module.system.service.a_audio_and_video.AudioAndVideoService;
import cn.iocoder.yudao.module.system.service.a_pictures.PicturesService;
import cn.iocoder.yudao.module.system.service.a_quest.QuestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 待办")
@RestController
@RequestMapping("/system/quest")
@Validated
public class QuestController {

    @Resource
    private QuestService questService;
    @Resource
    private ArticleService articleService;
    @Resource
    private AudioAndVideoService audioAndVideoService;
    @Resource
    private PicturesService picturesService;

    @PostMapping("/create")
    @Operation(summary = "创建待办")
    @PreAuthorize("@ss.hasPermission('system:quest:create')")
    public CommonResult<Integer> create(@Valid @RequestBody QuestCreateReqVO createReqVO) {
        return success(questService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新待办")
    @PreAuthorize("@ss.hasPermission('system:quest:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody QuestUpdateReqVO updateReqVO) {
        questService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除待办")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:quest:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
        questService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得待办")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<QuestRespVO> get(@RequestParam("id") Integer id) {
        QuestDO questDO = questService.get(id);
        return success(QuestConvert.INSTANCE.convert(questDO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得待办列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<List<QuestRespVO>> getList(@RequestParam("ids") Collection<Integer> ids) {
        List<QuestDO> list = questService.getList(ids);
        return success(QuestConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getEndTimeQuest")
    @Operation(summary = "获得马上逾期的待办列表")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<List<QuestRespVO>> getEndTimeQuest() {
        List<QuestDO> list = questService.getEndTimeQuest();
        return success(QuestConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getMonthQuest")
    @Operation(summary = "获得本月待办列表")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<List<QuestRespVO>> getMonthQuest() {
        List<QuestDO> list = questService.getMonthQuest();
        return success(QuestConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/selectPageSuccess")
    @Operation(summary = "获得待办分页")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<PageResult<QuestDO>> selectPageSuccess(@Valid QuestPageReqVO pageVO) {
        PageResult<QuestDO> pageResult = questService.selectPageSuccess(pageVO);
        //return success(QuestConvert.INSTANCE.convertPage(pageResult));
        return success(pageResult);
    }

    @GetMapping("/page")
    @Operation(summary = "获得待办分页")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<PageResult<QuestDO>> getPage(@Valid QuestPageReqVO pageVO) {
        PageResult<QuestDO> pageResult = questService.getPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出待办 Excel")
    @PreAuthorize("@ss.hasPermission('system:quest:export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid QuestExportReqVO exportReqVO,
                            HttpServletResponse response) throws IOException {
        List<QuestDO> list = questService.getList(exportReqVO);
        // 导出 Excel
        List<QuestExcelVO> datas = QuestConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "待办.xls", "数据", QuestExcelVO.class, datas);
    }

    //5条待办信息、 6条即将逾期信息、 49条新增数据
    @GetMapping("/countQuest")
    @Operation(summary = "获取待办及预期总数")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<Map<String, Integer>> countQuest() {
        Map<String, Integer> map = questService.countQuest();
        try {
            Long articleCount = articleService.insertArticleByDay();
            Long audioAndVideoCount = audioAndVideoService.insertAudioAndVideoByDay();
            Long picturesCount = picturesService.insertPicturesByDay();
            map.put("insertCount", Math.toIntExact(articleCount + audioAndVideoCount + picturesCount));
            map.put("articleCount", Math.toIntExact(articleCount));
            map.put("audioAndVideoCount", Math.toIntExact(audioAndVideoCount));
            map.put("picturesCount", Math.toIntExact(picturesCount));
        } catch (Exception e) {
            System.out.println("海贝错误");
        }
        //Map<String, Integer> map = new HashMap<>();
        //map.put("questCount", 2);
        //map.put("endTimeCount", 2);
        //map.put("insertCount", 90);
        //map.put("articleCount", 38);
        //map.put("audioAndVideoCount", 8);
        //map.put("picturesCount", 44);
        return success(map);
    }

    @PostMapping("/createByErp")
    @Operation(summary = "ERP待办")
    public CommonResult<Integer> createByERP(@RequestParam("questName") String questName,@RequestParam("erpId")String erpId) {
        return success(questService.createByERP(questName,erpId));
    }

    @GetMapping("/questList")
    @Operation(summary = "获取待办列表")
    @PreAuthorize("@ss.hasPermission('system:quest:query')")
    public CommonResult<PageResult<ResultTreeVO>> getQuestList(QuestListReqVO reqVO) {
        PageResult<ResultTreeVO> questList = questService.getQuestList(reqVO);
        return success(questList);
    }

    @GetMapping("/erpQuestList")
    @Operation(summary = "获取ERP待办")
    public CommonResult<PageResult<ResultTreeVO>> erpQuestList(QuestListReqVO reqVO) {
        PageResult<ResultTreeVO> erpQuestList = questService.getErpQuestList(reqVO);
        return success(erpQuestList);
    }

}
